<SettingsLayout page='settings/instances/add' label={pageLabel}>
  <h1 id="add-an-instance-h1">{pageLabel}</h1>

  <div class="add-new-instance">
    <form on:submit='onSubmitInstance(event)' aria-labelledby="add-an-instance-h1">

      {#if !hasIndexedDB || !hasLocalStorage}
        <div class="form-error form-error-user-error" role="alert">
          {intl.storageError}
        </div>
      {/if}

      {#if $logInToInstanceError && $logInToInstanceErrorForText === $instanceNameInSearch}
        <div class="form-error form-error-user-error" role="alert">
          {intl.errorShort} {@html $logInToInstanceError}
        </div>
      {/if}

      <noscript>
        <div class="form-error" role="alert">
          {intl.javaScriptError}
        </div>
      </noscript>

      <label for="instanceInput">{intl.instanceColon}</label>
      <input type="text" inputmode="url" autocapitalize="none" spellcheck="false" id="instanceInput"
             bind:value='$instanceNameInSearch' placeholder="{intl.enterInstanceName}" required
      >
      <button class="primary" type="submit" id="submitButton"
              disabled={!$instanceNameInSearch || $logInToInstanceLoading}>
        {intl.logIn}
      </button>
    </form>
  </div>

  {#if !$isUserLoggedIn}
    <p>
      {intl.getAnInstancePre}
      <Tooltip
        text="{intl.getAnInstanceText}"
        tooltipText="{intl.getAnInstanceDescription}"
      />
      {intl.getAnInstancePost}
      <a rel="noopener" target="_blank" href="https://joinmastodon.org">{intl.joinMastodon}</a>
    </p>
  {/if}
</SettingsLayout>
<style>
  .add-new-instance {
    background: var(--form-bg);
    padding: 5px 10px 15px;
    margin: 20px auto;
    border: 1px solid var(--form-border);
    border-radius: 4px;
  }

  .form-error {
    border: 2px solid var(--warn-color);
    border-radius: 2px;
    padding: 10px;
    font-size: 1.3em;
    margin: 5px;
    background-color: var(--main-bg);
  }
  input {
    min-width: 70%;
    max-width: 100%;
    background-color: var(--input-bg);
  }

  label, input, button, :global(.add-new-instance-aside) {
    display: block;
    margin: 20px 5px;
  }

  @media (max-width: 767px) {
    input {
      min-width: 95%;
    }
  }

</style>
<script>
  import SettingsLayout from '../../../_components/settings/SettingsLayout.html'
  import { store } from '../../../_store/store.js'
  import { logInToInstance, handleOauthCode } from '../../../_actions/addInstance.js'
  import { testHasIndexedDB, testHasLocalStorage } from '../../../_utils/testStorage.js'
  import Tooltip from '../../../_components/Tooltip.html'

  export default {
    async oncreate () {
      const params = new URLSearchParams(location.search)
      const code = params.get('code')
      if (code) {
        await handleOauthCode(code)
      } else {
        this.set({
          hasIndexedDB: await testHasIndexedDB(),
          hasLocalStorage: testHasLocalStorage()
        })
      }
    },
    components: {
      SettingsLayout,
      Tooltip
    },
    store: () => store,
    data: () => ({
      hasIndexedDB: true,
      hasLocalStorage: true
    }),
    computed: {
      pageLabel: ({ $isUserLoggedIn }) => $isUserLoggedIn ? 'intl.addInstance' : 'intl.logIn'
    },
    methods: {
      onSubmitInstance (event) {
        event.preventDefault()
        event.stopPropagation()
        logInToInstance()
      }
    }
  }
</script>
